package com.wavehi.interceptior;

import com.alibaba.druid.sql.ast.SQLDeclareItem;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;

import java.util.Properties;

/**
 * @Description Change parameter after prepare statement.
 * @Author WaveHi
 * @Date 1/12/2021 下午 7:03
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
@Intercepts({
        @Signature(
                type = StatementHandler.class,
                method = "parameterize",
                args = java.sql.Statement.class
        )
})
public class PluginForChange implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        //获取元数据
        MetaObject metaObject = SystemMetaObject.forObject(invocation.getTarget());
        //通过元数据设置对象属性值
        metaObject.setValue("parameterHandler.parameterObject", 3L);
        Object proceed = invocation.proceed();
        return proceed;
    }
    @Override
    public Object plugin(Object target) {
        Object wrap = Plugin.wrap(target, this);
        return wrap;
    }
    @Override
    public void setProperties(Properties properties) {
        System.out.println("Plugin for change!");
    }
}
